package com.ttbj.controller.sysuser;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.constant.DriverEnum;
import com.ttbj.product.api.AppletUserFacade;
import com.ttbj.product.api.DriverFacade;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.Assert;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.SafetyUtils;
import com.ttbj.util.ViewModelUtil;
import cn.jiguang.common.utils.StringUtils;

@Controller
@RequestMapping("/driver/user")
public class DriverController {
	private static final Logger logger = LoggerFactory.getLogger(DriverController.class);

	@Autowired
	AppletUserFacade appletUserFacade = null;

	@Autowired
	DriverFacade driverFacade = null;

	/**
	 * 获取短信（短运司机短信获取）
	 * 
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping(value = "/sms.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData driverSms(HttpServletRequest request, @RequestParam("phone") String driverPhone) throws Exception {
		if (!StringUtils.isMobileNumber(driverPhone)) {
			logger.info("手机号码格式错误");
			return ResultUtil.createFail("messages", 911, "手机号码" );
		}
		appletUserFacade.getSms(driverPhone);
		return ViewModelUtil.buildSuccess("成功");
	}

	/**
	 * 短信验证，登陆/注册（短运司机通过短信验证登录或是注册） 1
	 * 
	 * @param request
	 *            请求
	 * @param phone
	 *            电话
	 * @param code
	 *            验证码
	 * @throws Exception
	 */
	@RequestMapping(value = "/sms/login.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData login(HttpServletRequest request, String phone, String code) throws Exception {
		if (!appletUserFacade.verifySms(phone, code)) {
			return ResultUtil.createFail("messages", 914);
		}
		return driverFacade.login(new DriverDto().phone(phone));
	}
	
	/**
	 * 登出接口
	 * @param request
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData logout(HttpServletRequest request, String token) throws Exception {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		driverFacade.logout(driverDto);
		return null;
	}

	/**
	 * 密码补全 2
	 * 
	 * @param driver
	 *            司机信息
	 * @param token
	 */
	@RequestMapping(value = "/cmpl/pwd.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData cmplPwd(HttpServletRequest request, DriverDto driver, String token) throws Exception {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		Assert.assertNonNullField(driver, "密码参数出现错误,司机资料信息", 125, null, "driverPwd");
		driver.setDriverPwd(SafetyUtils.addSalt(driver.getDriverPwd(), driverDto.getDriverName()));
		Integer driverIsCompany = driverDto.getDriverIsCompany();
		if (driverIsCompany == 0) {
			driver.driverFlag(DriverEnum.TRUCK.getValue()).setDriverId(driverDto.getDriverId());
		} else {
			driver.driverFlag(DriverEnum.PWD.getValue()).setDriverId(driverDto.getDriverId());
		}
		return driverFacade.cmplData(driver.isCompany(driverIsCompany));
	}

	/**
	 * 短运司机车辆信息补全 3
	 * 
	 * @param phone
	 *            电话
	 * @param carNum
	 *            车牌号
	 * @throws Exception
	 */
	@RequestMapping(value = "/cmpl/truck.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData cmplTruck(HttpServletRequest request, String carNum, String truckType, String token) throws Exception {
		DriverDto driverDto = ((DriverDto) request.getAttribute("user")).driverFlag(DriverEnum.TRUCK.getValue());
		return driverFacade.inputTruck(driverDto, carNum, truckType);
	}

	/**
	 * 短运司机身份证补全 4
	 * 
	 * @param driver
	 *            司机名字 ， 司机身份证号等信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/cmpl/idcard.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData cmplIDcard(HttpServletRequest request, DriverDto driver, String token) throws Exception {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		Assert.assertNonNullField(driver, "身份证参数出现错误,司机资料信息", 216, null, "driverIdcard");
		driver.driverFlag(DriverEnum.IDCARD.getValue()).setDriverId(driverDto.getDriverId());
		return driverFacade.cmplData(driver);
	}

	/**
	 * 短运司机驾驶证和行驶证补全 5
	 * 
	 * @param driver
	 *            driverVehiclePic driverLicencePic
	 * @throws Exception
	 */
	@RequestMapping(value = "/cmpl/imageUrl.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData cmplUrl(HttpServletRequest request, String token, DriverDto driver) throws Exception {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		Assert.assertNonNullField(driver, "驾驶证或行驶证参数出现错误,司机资料信息", 912, null, "driverVehiclePic", "driverLicencePic");
		driver.driverFlag(DriverEnum.LICENCE.getValue()).setDriverId(driverDto.getDriverId());
		return driverFacade.cmplData(driver);
	}
	
	

	/**
	 * 短信验证，修改联系方式 1.验证新手机 2.携带新手机
	 * 
	 * @param phone
	 *            手机号码
	 * @param code
	 *            验证码
	 */
	@RequestMapping(value = "/sms/update/phone.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData updatePhone(HttpServletRequest request, String phone, String code, String token) throws Exception {
		if (!appletUserFacade.verifySms(phone, code)) {
			return ResultUtil.createFail("messages", 914);
		}
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		driverDto.setDriverPhone(phone);
		driverDto.setDriverName(phone);
		return driverFacade.cmplData(driverDto);
	}
	
	
	 /**
	  * 获取司机信息
	  * @param request
	  * @param token
	  * @return
	  */
	@RequestMapping(value = "/info.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData driverInfo(HttpServletRequest request, String token){
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		return ViewModelUtil.buildSuccess(0, "成功", driverDto);
	}
	
	
	
	
	

}
